package com.sphd.restaurantsystem.activitys.marking;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.adapters.JifenCommodityAdapter;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.DuihuanInfo;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.JifenCommodityPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.JifenCommodityView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JifenCommodityActivity extends BaseActivity implements JifenCommodityView {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.my_recy)
    RecyclerView myRecy;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    JifenCommodityPresenterImpl jifenCommodityPresenter;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    JifenCommodityAdapter jifenCommodityAdapter;
    @BindView(R.id.btn_id)
    ImageView btnId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jifen_commodity);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        mySwf.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                addData();
            }
        });
        toolbar.setNavigationOnClickListener(view -> finish());
        myRecy.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void SimpleOnItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {

            }

            @Override
            public void onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                super.onItemLongClick(adapter, view, position);
                if (isCg_has_storemanager(restaurantInfo) && isSuperManager(loginInfo)) {
                    toast("店长会去操作，您不用费心了");
                } else {
                    openDialog(position);
                }
            }
        });

    }

    private void openDialog(int position) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                context);
        alertDialogBuilder
                .setTitle("删除提示")
                .setMessage("你将删除本条积分信息")
                .setCancelable(false)
                .setPositiveButton("确定",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                jifenCommodityPresenter.delUrl(RestaurantUrl.Httptitle + RestaurantUrl.Deljifen
                                        + "id=" + jifenCommodityAdapter.getItem(position).getId());
                            }
                        })
                .setNegativeButton("取消",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });

        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        loginInfo = DataSupport.findLast(LoginInfo.class);
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        jifenCommodityPresenter = new JifenCommodityPresenterImpl(this);
        setTitleBar(toolbar, "积分商品");
        inAdapter();
        addData();
    }

    private void inAdapter() {
        setRefresh(mySwf);
        setRecy(myRecy);
        jifenCommodityAdapter = new JifenCommodityAdapter(R.layout.layout_jifen_item, new ArrayList<>());
        myRecy.setAdapter(jifenCommodityAdapter);
    }

    private void addData() {
        jifenCommodityPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.Jifenlist + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recList(List<DuihuanInfo> duihuanInfos) {
        jifenCommodityAdapter.setNewData(duihuanInfos);
        mySwf.setRefreshing(false);
    }

    @Override
    public void recRes(int res) {
        if (res == 1) {
            toast("删除成功");
            addData();
        } else {
            toast("删除失败，请重试！");

        }
    }

    @OnClick(R.id.btn_id)
    public void onClick() {
        intent.setClass(context, AddJifenActivity.class);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            addData();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
